package com.base.cn.platform.os.controller.user.integral;

import com.base.cn.platform.os.common.controller.BaseController;
import com.base.cn.platform.os.common.mybatis.Pagination;
import com.base.cn.platform.os.common.utils.ObjectUtils;
import com.base.cn.platform.os.entity.user.integral.CusUserIntegralRecord;
import com.base.cn.platform.os.service.user.integral.CusUserIntegralBiz;
import com.base.cn.platform.os.service.user.integral.CusUserIntegralRecordBiz;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Data 用户积分记录
 * @ClassName  CusUserIntegralRecordController
 * @author :ll
 * @Create Date : 2018年4月24日
 */
@RestController
@RequestMapping("/manage/user")
public class CusUserIntegralRecordController extends BaseController {

    @Autowired
    private CusUserIntegralRecordBiz cusUserIntegralRecordBiz;
    @Autowired
    private CusUserIntegralBiz cusUserIntegralBiz;

    /**
     * 搜索积分记录
     * @param containColumns
     * @param cusUserIntegralRecord
     * @return
     */
    @RequestMapping("/queryCusUserIntegralRecordList")
    public List<CusUserIntegralRecord> queryCusUserIntegralRecordList(@RequestParam(value = "count",required = false)Integer count,
                                                                      @RequestParam(value = "containColumns",required = false)List<String> containColumns,
                                                                      @RequestBody CusUserIntegralRecord cusUserIntegralRecord,
                                                                      @RequestParam(value = "integralRule",required = false,defaultValue = "false")boolean integralRule){
        Pagination page = new Pagination();
        page.setCurrentPage(1);
        return cusUserIntegralRecordBiz.queryCusUserIntegralRecordList(count,cusUserIntegralRecord,containColumns,integralRule);
    }




    /**
     * 搜索积分记录
     * @param currentPage
     * @param cusUserIntegralRecord
     * @return
     */
    @RequestMapping("/queryCusUserIntegralRecordListPage")
    public PageInfo<CusUserIntegralRecord> queryCusUserIntegralRecordListPage(@RequestParam(value = "currentPage",required = false,defaultValue = "1")Integer currentPage,
                                                                              @RequestBody CusUserIntegralRecord cusUserIntegralRecord,
                                                                              @RequestParam(value = "integralRule",required = false,defaultValue = "false")boolean integralRule){
        Pagination page = new Pagination();
        page.setCurrentPage(currentPage);
        return cusUserIntegralRecordBiz.queryCusUserIntegralRecordPage(page,cusUserIntegralRecord,null,integralRule);
    }


    /**
     * 获取积分统计信息
     * @return
     */
    @RequestMapping("/getIntegralStatis")
    Map<String,Object> getIntegralStatis(){
        CusUserIntegralRecord cusUserIntegralRecord = cusUserIntegralRecordBiz.findOne(" 1=1  order by createTime desc ", null);
        BigDecimal protectScore = cusUserIntegralBiz.sum("protectScore", null);
        BigDecimal exchangeNum = cusUserIntegralBiz.sum("exchangeNum", null);
        BigDecimal changeAmount = cusUserIntegralBiz.sum("changeAmount", null);


        Map<String, Object> result = new HashMap<>();
        if (ObjectUtils.isNotEmpty(cusUserIntegralRecord)) {
            result.put("useScore", cusUserIntegralRecord.getUseScore());
            result.put("consumeScore", cusUserIntegralRecord.getConsumeScore());
            result.put("totalScore", cusUserIntegralRecord.getTotalScore());
            result.put("protectScore", protectScore);
            result.put("exchangeNum", exchangeNum);
            result.put("changeAmount", changeAmount);
        }else{
            result.put("useScore", 0);
            result.put("consumeScore", 0);
            result.put("totalScore", 0);
            result.put("protectScore", 0);
            result.put("exchangeNum", 0);
            result.put("changeAmount", 0);
        }
        return result;
    }

}
